package com.zbkj.admin.front;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.IntStream;

public class DividendAllocation {
    public static void main(String[] args) {
        final double INIT_POOL = 30D;     // 初始分红池
        final double GROWTH_RATE = BigDecimal.valueOf(15)
                .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_DOWN)
                .subtract(BigDecimal.valueOf(0.00057D)).setScale(5, RoundingMode.HALF_DOWN).doubleValue();    // 每期增幅15%
        final int TARGET_PERIOD = 36;       // 目标期数

        var formatter = NumberFormat.getNumberInstance();
        AtomicReference<Double> pool = new AtomicReference<>(INIT_POOL);
        AtomicReference<Double> total = new AtomicReference<>(0.0);

        // 生成表格头
        System.out.println("""
                期数\t增幅\t分红池数量\t消费券（商家/用户）\t累计分红
                ------------------------------------------------------------""");

        // 生成表格内容
        IntStream.rangeClosed(1, TARGET_PERIOD).forEach(period -> {
            double coupon = pool.get() * 0.01;
            double currentTotal = total.get() + coupon;

            // 动态高亮判断
            String highlight = period == TARGET_PERIOD ? "\u001B[31m" : "";
            String reset = period == TARGET_PERIOD ? "\u001B[0m" : "";

            // 格式化工整输出
            System.out.printf("""
                            %s%02d\t%.0f%%\t%-12s\t%-18s\t%s%s%n""",
                    highlight,
                    period,
                    GROWTH_RATE * 100,
                    formatter.format(pool.get()),
                    formatter.format(coupon / 2),
                    formatter.format(currentTotal),
                    reset
            );

            // 更新池和累计值（最后一期不增长） new Double((v * (1 + GROWTH_RATE)))
            if (period < TARGET_PERIOD) {
                pool.updateAndGet(v -> v * (1 + GROWTH_RATE));
                total.set(currentTotal);
            }
        });

        // 验证第36期累计分红结果
//        System.out.printf("%n最终累计分红：%.2f元 %s目标值2000元",
//            totalDividend,
//            totalDividend >= 2000 ? "≥" : "<");
    }
}